# -*- coding: utf-8 -*-


import csv
import pandas as pd


source_path = "./DZ.xlsx"
target_path = "./DZ_result.csv"
save_path = "./DZ_1.csv"

source_list = []
target_list = []
target_value_list = []
null_list = []

df_s = pd.read_excel(source_path, header=None)
df_t = pd.read_csv(target_path, header=None)

for index, row in df_s.iterrows():
    source_list.append(row[0].strip())

for index, row in df_t.iterrows():
    target_list.append(row[0].strip())
    target_value_list.append(str(row[1]).strip())


# for index_s, value_s in enumerate(source_list):
#     for index_t, value_t in enumerate(target_list):
#         if value_s == value_t:
#             break


source_list_count = len(source_list)
target_list_count = len(target_list)
print(f"Source: {source_list_count}")
print(f"Target: {target_list_count}")

# # 对比不同的数据
for i in range(0, source_list_count):
    if source_list[i] == target_list[i]:
        pass
    else:
        print(f"Line:{i} Source: {source_list[i]}===Line:{i} Target: {target_list[i]}")
        break
print("=======对比结束==========")

# 根据基础数据source_path查找已经搜索完的记录target_path。并保存新的文件save_path
for index_s, value_s in enumerate(source_list):
    flag = True
    for index_t, value_t in enumerate(target_list):
        if value_s == value_t:
            flag = False
            with open(save_path, 'a+', encoding="utf-8-sig", newline='') as f:
                csv_writer = csv.writer(f)
                csv_writer.writerow([value_s, target_value_list[index_t]])
            break
    if flag == True:
        print(f"==缺失=={index_s + 1}======{value_s}")
        null_list.append((value_s))
        with open(save_path, 'a+', encoding="utf-8-sig", newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([value_s, ""])

print(f"=======结束===缺失:{len(null_list)}=======")

